<?php
    include '../connectDatabase.php';

    session_start();

    $ID = $_POST['ID'];
    $passwd = $_POST['passwd'];

    $sql = "select * from users where user_id='$ID' and user_password='$passwd'";

    $result = mysqli_query($conn,$sql);
    $records = array();
    while($record = mysqli_fetch_array($result))
    {
        $records [] = $record;
    }
    foreach($records as $row){
        $type = $row['user_type'];
    }

    if($records == null){
        echo "<script>alert('用户名不存在或密码错误！');window.location.href='http://localhost:8080/PHP-NEWS-RELEASE-SYSTEM/Application/User/Page/login.php'</script>";    
    } else if($type=="管理员"){
        $_SESSION['userid'] = $ID;
        $_SESSION['type'] = "管理员";
        echo "<script>alert('管理员，欢迎您！');window.location.href='http://localhost:8080/PHP-NEWS-RELEASE-SYSTEM/Application/User/Page/login.php'</script>";
    }
    else if($type=="超级管理员"){
        $_SESSION['userid'] = $ID;
        $_SESSION['type'] = "超级管理员";
        //echo "<a href='http://localhost:8080/PHP-NEWS-RELEASE-SYSTEM/Application/User/Page/admin_modify.php'>还没有账户?</a>";
        echo "<script>alert('尊敬的超级管理员，欢迎您！');window.location.href='http://localhost:8080/PHP-NEWS-RELEASE-SYSTEM/Application/User/Page/login.php'</script>";
    } else{
        $_SESSION['userid'] = $ID;
        $_SESSION['type'] = "普通用户";
        echo "<script>alert('登录成功！');window.location.href='http://localhost:8080/PHP-NEWS-RELEASE-SYSTEM/Application/User/Page/login.php'</script>";   
    }

    
?>